namespace ImapX
{
    using System;
    using System.Collections.Generic;

    internal static class ParseHelper
    {
        public static MailAddress Address(string line)
        {
            int startIndex = line.LastIndexOf("<");
            if (startIndex < 0)
            {
                return new MailAddress(null, line.Trim());
            }
            string addr = line.Substring(startIndex).Trim().TrimStart(new char[] { '<' }).TrimEnd(new char[] { '>' });
            string display = "";
            if (startIndex >= 1)
            {
                display = line.Substring(0, startIndex - 1).Trim();
            }
            return new MailAddress(display, addr);
        }

        public static List<MailAddress> AddressCollection(string value)
        {
            List<MailAddress> list = new List<MailAddress>();
            foreach (string str in value.Trim().Split(new string[] { ">,", "> ," }, StringSplitOptions.None))
            {
                try
                {
                    list.Add(Address(str));
                }
                catch (Exception)
                {
                    throw new Exception("Not correct mail address");
                }
            }
            return list;
        }

        public static bool Exists(string line, ref int property)
        {
            int num;
            if (!line.Contains("EXISTS"))
            {
                return false;
            }
            if (int.TryParse(line.Split(new char[] { ' ' })[1], out num))
            {
                property = num;
            }
            return true;
        }

        public static bool MessageProperty(string key, string value, string header, ref string property)
        {
            if (key.ToLower().Trim().Equals(header))
            {
                property = value;
                return true;
            }
            return false;
        }

        public static bool Recent(string line, ref int property)
        {
            int num;
            if (!line.Contains("RECENT"))
            {
                return false;
            }
            if (int.TryParse(line.Split(new char[] { ' ' })[1], out num))
            {
                property = num;
            }
            return true;
        }

        public static bool UidNext(string line, ref int property)
        {
            int num;
            if (!line.Contains("UIDNEXT"))
            {
                return false;
            }
            if (int.TryParse(line.Split(new char[] { ' ' })[3].Replace("]", ""), out num))
            {
                property = num;
            }
            return true;
        }

        public static bool UidValidity(string line, ref string property)
        {
            if (line.Contains("UIDVALIDITY"))
            {
                string str = line.Split(new char[] { ' ' })[3].Replace("]", "");
                property = str;
                return true;
            }
            return false;
        }

        public static bool Unseen(string line, ref int property)
        {
            int num;
            if (!line.Contains("UNSEEN"))
            {
                return false;
            }
            if (int.TryParse(line.Split(new char[] { ' ' })[3].Replace("]", ""), out num))
            {
                property = num;
            }
            return true;
        }
    }
}

